<?php
class KyduyetController extends AppController {
	public $uses = array('Hoso','Tthc','ThanhPhan','ThanhPho','ThanhPhanTthc','Status','User', 'LichsuHoso','Attachment','PhieuTrinh','Ketquahanhchinh');
	
	public function beforeFilter() {
		$this->layout = 'quanly';
	}
	
	public function index($status = null) {
		$st = array(
			$this->Status->HS_CHO_KY,
			$this->Status->HS_DA_KY,
			$this->Status->HS_KHONG_DUOC_KY);
			
		if (!empty($status) && in_array($status, $st)) {
			$st = array($status);
		}
		
		$listStatus = array(
			1 => 'All',
			$this->Status->HS_CHO_KY => 'Hồ sơ chờ ký',
			$this->Status->HS_DA_KY => 'Hồ sơ đã ký',
			$this->Status->HS_KHONG_DUOC_KY => 'Hồ sơ không được ký');
			
		$this->set('listStatus', $listStatus);
		$this->set('status', $status);
		
		$listHosos = $this->Hoso->find("all", array(
			'conditions' => array('Hoso.trang_thai' => $st, 'Hoso.nguoi_ky_id'  => $this->Auth->user('id')),
			'order' => array('Hoso.ngay_dang_ky' => 'desc')
		));
		
		$this->set('listHosos', $listHosos);
	}
	
	public function kyDuyet ($id = null) {
		$inStatus = array(
			$this->Status->HS_CHO_KY,
			$this->Status->HS_DA_KY,
			$this->Status->HS_KHONG_DUOC_KY);
			
		$hoso = $this->Hoso->findById($id);
		
		$thanhPhos = $this->ThanhPho->find("list", array('fields'=>array('code','ten')));
		$this->set('thanhPhos', $thanhPhos);
		
		$this->User->recursive = -1;
		
		$users = $this->User->find("list", array("conditions" => array("User.username !=" => 'admin'), 'fields'=>array('id','username')));
		$his = $this->LichsuHoso->find("all", array("conditions" => array("LichsuHoso.hoso_id" => $id)));
		
		$this->set("enable", 0);
		
		if(in_array($hoso['Hoso']['trang_thai'], $inStatus)) {
			$this->set("enable", 1);
		}
		
		// calculate ngay qua han
		$date1 = new DateTime();
		$date2 = new DateTime($hoso['Hoso']['ngay_hen_tra']);
		($this->compareDate($date1, $date2) < 0) && $this->set("qua_han", $this->compareDate($date1, $date2));
		
		// debug($users);
		$this->set("his", $his);
		$this->set("hoso", $hoso);
		$this->set("users", $users);
		$this->set("selectedHoso", $id);
	}
	
	public function ky ($id = null) {
		if (!empty($this->request->data)) {
			$hoso = $this->Hoso->findById($this->request->data['Hoso']['id']);
			$date1 = new DateTime();
			$date2 = new DateTime($hoso['Hoso']['ngay_hen_tra']);
			
			$this->Hoso->id = $this->request->data['Hoso']['id'];
			$this->Hoso->saveField('trang_thai', $this->Status->HS_DA_KY);
			$this->Hoso->saveField('editable', 0);
			//$this->Hoso->saveField('so_ngay_con_lai', $this->compareDate($date1, $date2));
			$this->Hoso->save();
			
			// update history
			
			$his = array(); $his['LichsuHoso'] = array();
			
			$his['LichsuHoso']['user_id'] = $this->Auth->user('id');
			$his['LichsuHoso']['hoso_id'] = $this->Hoso->id;
			$his['LichsuHoso']['noi_dung_giai_quyet'] = 'Ký duyệt hồ sơ';
			$his['LichsuHoso']['thao_tac'] = 'Ky duyệt hồ sơ';
			$his['LichsuHoso']['so_ngay_con_lai'] = $this->compareDate($date1, $date2);
			
			$his['LichsuHoso']['status'] = $this->Status->HS_DA_KY;
			$this->LichsuHoso->create();
			$this->LichsuHoso->save($his);
			
			$this->redirect("index");
		}
	}
	
	public function khongky ($id = null) {
		if (!empty($this->request->data)) {
			$hoso = $this->Hoso->findById($this->request->data['Hoso']['id']);
			$date1 = new DateTime();
			$date2 = new DateTime($hoso['Hoso']['ngay_hen_tra']);
			
			$this->Hoso->id = $this->request->data['Hoso']['id'];
			//$this->Hoso->saveField('so_ngay_con_lai', $this->compareDate($date1, $date2));
			$this->Hoso->saveField('trang_thai', $this->Status->HS_KHONG_DUOC_KY);
			
			$this->Hoso->save();
			
			// update history
			
			
			$his = array(); $his['LichsuHoso'] = array();
			$his['LichsuHoso']['user_id'] = $this->Auth->user('id');
			$his['LichsuHoso']['hoso_id'] = $this->Hoso->id;
			$his['LichsuHoso']['noi_dung_giai_quyet'] = 'Không ký hồ sơ';
			$his['LichsuHoso']['thao_tac'] = 'Không ký hồ sơ';
			$his['LichsuHoso']['so_ngay_con_lai'] =  $this->compareDate($date1, $date2);
			
			$his['LichsuHoso']['status'] = $this->Status->HS_KHONG_DUOC_KY;
			$this->LichsuHoso->create();
			$this->LichsuHoso->save($his);
			
			$this->redirect("index");
		}
	}
	
	/**
	 * Xem Hoso truoc de xuly
	 */
	public function xemHoso ($id = null) {
		if (!empty($this->request->data)) {
			$date = !empty($this->request->data['Hoso']['ngay_tra'])?$this->convertStrDate($this->request->data['Hoso']['ngay_tra']): NULL;
			
			$this->Hoso->id = $this->request->data['Hoso']['id'];
			$this->Hoso->saveField('ghi_chu', $this->request->data['Hoso']['ghi_chu']);
			!empty($date)?$this->Hoso->saveField('ngay_hen_tra', $date):'';
			$this->Hoso->save();
			
			$this->redirect(array("action" => "xuly",$this->Hoso->id));
		}
		
		$hoso = $this->Hoso->findById($id);
		$thanhPhos = $this->ThanhPho->find("list", array('fields'=>array('code','ten')));
		$this->set('thanhPhos', $thanhPhos);
		
		if (!empty($hoso)){
			$thanhPhans = $this->ThanhPhan->getListThanhPhanGiayToByTthcId($hoso['Tthc']['id']);
			$this->set("thanhPhans", $thanhPhans);
			
			$attachments = $this->Attachment->find("all", array('conditions' => array("hoso_id" => $id)));
			$this->set("attachments", $attachments);
		} else {
			$this->log("Invalid document Id:"+$id);
		}
		
		$hoso['Hoso']['ngay_tra'] = empty($hoso['Hoso']['ngay_hen_tra'])?"":CakeTime::format('d/m/Y',$hoso['Hoso']['ngay_hen_tra']);
		//export value to view
		$this->set("hoso", $hoso);
		$this->set("selectedHoso", $id);
	}

	public function lapPhieuTrinh($id = null) {
		
		if (!empty($this->request->data)) {
			$this->log("Update new phieu trinh", 'info');
			
			if(isset($this->request->data['PhieuTrinh']['ngay_form'])) {
				$this->request->data['PhieuTrinh']['ngay'] = $this->convertStrDate($this->request->data['PhieuTrinh']['ngay_form']);
			}

			$phieu = $this->PhieuTrinh->findByHosoId($id);
			if (empty($phieu)) {
				$phieu = array();
				$phieu['PhieuTrinh'] = array();
			} 
			
			$phieu['PhieuTrinh']['hoso_id'] = $this->request->data['PhieuTrinh']['hoso_id'];
			$phieu['PhieuTrinh']['so'] = $this->request->data['PhieuTrinh']['so'];
			$phieu['PhieuTrinh']['ngay'] = $this->request->data['PhieuTrinh']['ngay'];
			$phieu['PhieuTrinh']['kinh_gui'] = $this->request->data['PhieuTrinh']['kinh_gui'];
			$phieu['PhieuTrinh']['can_bo_trinh'] = $this->request->data['PhieuTrinh']['can_bo_trinh'];
			$phieu['PhieuTrinh']['ve_viec'] = $this->request->data['PhieuTrinh']['ve_viec'];
			$phieu['PhieuTrinh']['y_kien_ca_bo_xuly'] = $this->request->data['PhieuTrinh']['y_kien_ca_bo_xuly'];
			$phieu['PhieuTrinh']['y_kien_kiem_duyet'] = $this->request->data['PhieuTrinh']['y_kien_kiem_duyet'];
			$phieu['PhieuTrinh']['y_kien_lanh_dao'] = $this->request->data['PhieuTrinh']['y_kien_lanh_dao'];
			
			if(isset($this->request->data['PhieuTrinh']['ngay_form'])) {
				$this->request->data['PhieuTrinh']['ngay'] = $this->convertStrDate($this->request->data['PhieuTrinh']['ngay_form']);
			} 
			
			//debug($phieu);
			
			$this->PhieuTrinh->save($phieu);
			
			$this->redirect(array('action' => 'kyduyet', $id));
		}

		if (!empty($id)) {
			$phieu = $this->PhieuTrinh->findByHosoId($id);
			//debug($phieu);
			
			$this->request->data['PhieuTrinh'] = $phieu["PhieuTrinh"];
			if(!isset($phieu["PhieuTrinh"]['ngay'])) {
				$this->set('ngay',date('d/m/Y'));
			}
			
			$hoso = $this->Hoso->findById($id);
			$this->set("editable", $this->isEdit($hoso));
		}
		
		$this->set("selectedHoso", $id);
	}

	public function ketquahanhchinh ($id = null) {
		$hoso = $this->Hoso->findById($id);
		
		$ketquas = $this->Ketquahanhchinh->find("all", array("conditions" => array("Ketquahanhchinh.hoso_id" => $id)));
		
		
		// calculate ngay qua han
		$date1 = new DateTime();
		$date2 = new DateTime($hoso['Hoso']['ngay_hen_tra']);
		
		($this->compareDate($date1, $date2) < 0) && $this->set("qua_han", $this->compareDate($date1, $date2));
		
		$this->set("hoso", $hoso);
		$this->set("ketquas", $ketquas);
		$this->set("selectedHoso", $id);
	}
}
